package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysSlideshowBo;
import org.dromara.system.domain.vo.SysSlideshowVo;
import org.dromara.system.domain.SysSlideshow;
import org.dromara.system.mapper.SysSlideshowMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 轮播图Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-03-23
 */
@RequiredArgsConstructor
@Service
public class SysSlideshowServiceImpl extends BaseService {

    private final SysSlideshowMapper baseMapper;

    /**
     * 查询轮播图
     */
    public SysSlideshowVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询轮播图列表
     */
    public TableDataInfo<SysSlideshowVo> queryPageList(SysSlideshowBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSlideshow> lqw = buildQueryWrapper(bo);
        Page<SysSlideshowVo> result = baseMapper.getSlideshowList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询轮播图列表
     */
    public List<SysSlideshowVo> queryList(SysSlideshowBo bo) {
        LambdaQueryWrapper<SysSlideshow> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSlideshow> buildQueryWrapper(SysSlideshowBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSlideshow> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPage()), SysSlideshow::getPage, bo.getPage());
        lqw.eq(bo.getPictureId() != null, SysSlideshow::getPictureId, bo.getPictureId());
        return lqw;
    }

    /**
     * 新增轮播图
     */
    public Boolean insertByBo(SysSlideshowBo bo) {
        SysSlideshow add = MapstructUtils.convert(bo, SysSlideshow.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改轮播图
     */
    public Boolean updateByBo(SysSlideshowBo bo) {
        SysSlideshow update = MapstructUtils.convert(bo, SysSlideshow.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysSlideshow entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除轮播图
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
